package com.springapp.mvc.controller;

import com.springapp.mvc.dao.bean.Operation;
import com.springapp.mvc.params.OperationInsertParam;
import com.springapp.mvc.params.OperationQueryParam;
import com.springapp.mvc.resultBean.BaseResult;
import com.springapp.mvc.resultBean.CommonDatatableBean;
import com.springapp.mvc.resultBean.OperationDeleteBean;
import com.springapp.mvc.resultBean.ResultCode;
import com.springapp.mvc.service.Interface.OperationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by qinyy on 2018/8/14.
 */
@Controller
@RequestMapping("/operation")
public class OperationController
{
    @Autowired(required = false)
    OperationService mOperationService;

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST,value = "insert")
    public BaseResult insertOperation(OperationInsertParam param)
    {
        if(param == null)
            return null;
        Operation op = new Operation();
        if("1".equalsIgnoreCase(param.getNeeduploadlocation()))
        {
            op.setNeeduploadlocation(true);
        }
        else
        {
            op.setNeeduploadlocation(false);
        }

        if("1".equalsIgnoreCase(param.getNeeduploadrecords()))
        {
            op.setNeeduploadrecords(true);
            op.setRecordsbegindate(param.getRecordsbegindate());
            op.setRecordsenddate(param.getRecordsenddate());
        }
        else
        {
            op.setNeeduploadrecords(false);
        }

        if("1".equalsIgnoreCase(param.getNeeduploadsounds()))
        {
            op.setNeeduploadsounds(true);
            op.setUploadsoundsids(param.getUploadsoundsids());
        }
        else
        {
            op.setNeeduploadsounds(false);
        }
        op.setIsexcuted(false);
        try
        {
            mOperationService.insertNewOperation(op);
            BaseResult result = new BaseResult();
            result.setCode("0");
            return result;
        }
        catch (Exception e)
        {
            BaseResult result = new BaseResult();
            result.setMessage(e.getMessage());
            result.setCode("1");
            return result;
        }
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.GET,value = "delete")
    public BaseResult deleteOperation(int operationId)
    {
        BaseResult result = new BaseResult();
        OperationDeleteBean bean = new OperationDeleteBean();
        int ret = mOperationService.deleteOperation(operationId);
        bean.setOperationId(operationId);
        result.setData(bean);
        result.setCode("0");
        return result;
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST,value = "query")
    public BaseResult queryOperation(OperationQueryParam param)
    {
        if(param == null)
            return null;
        BaseResult result = new BaseResult();
        CommonDatatableBean tableBean = new CommonDatatableBean();
        tableBean.setDraw(param.getDraw());
        int totalAmount = mOperationService.getOperationCount();
        tableBean.setRecordsTotal(totalAmount);
        tableBean.setRecordsFiltered(totalAmount);
        int limit = param.getStart();
        List<Object> list = new ArrayList<Object>();
        String sortType = param.getSortType() == 0?"ASC":"DESC";
        List<Operation> operations = mOperationService.queryOperationPaged(limit,param.getOffset(),param.getSortPro(),sortType);
        if(operations != null && operations.size() > 0)
        {
            for(Operation operation:operations)
            {
                list.add(operation);
            }
        }
        tableBean.setData(list);
        result.setData(tableBean);
        result.setResult(ResultCode.SUCCESS);
        return result;
    }
}
